package cn.itcast.utils;

/**
 *   VO     值对象,页面对象 view Object / value Object
 *   BO     service 业务用的对象  , 服务之间的调用
 *   DTO     data transfer Object  数据传输对象 设置validate 注解 AOP
 *   POJO     持久对象
 *
 */

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class XSSF {

    public void getExcel(){
        //创建工作本
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //读取第一个表
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        //获取第一行数据
        sheet.getFirstRowNum();
        System.out.println("第一行行数: "+sheet.getFirstRowNum());
        //获取最后一行的数据
        int lastRowNum = sheet.getLastRowNum();
        System.out.println("最后一行的行数: "+lastRowNum);
        //遍历最后一行的数据
        for (int i = 0; i <=lastRowNum ; i++) {
            XSSFRow row = sheet.getRow(i);
            row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                row.getCell(j).getStringCellValue();
            }

        }

    }

    public void writeExcel() throws IOException {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet userSheet = xssfWorkbook.createSheet("UserSheet");
        XSSFRow row1 = userSheet.createRow(0);
        row1.createCell(0).setCellValue("username");
        row1.createCell(1).setCellValue("sex");
        row1.createCell(2).setCellValue("hobby");

        XSSFRow row2 = userSheet.createRow(1);
        row2.createCell(0).setCellValue("傲霸");
        row2.createCell(1).setCellValue("公");
        row2.createCell(2).setCellValue("挑衅吾皇");

        XSSFRow row3 = userSheet.createRow(2);
        row3.createCell(0).setCellValue("牛能");
        row3.createCell(1).setCellValue("公");
        row3.createCell(2).setCellValue("撩妹");

        String bookPath = this.getClass().getResource("/").getPath();
        FileOutputStream outputStream = new FileOutputStream(bookPath + "new excel");
        xssfWorkbook.write(outputStream);



    }


}
